package by.grsu.transport.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

@SuppressWarnings("serial")
public class RegistrationForm extends ActionForm {

	private String firstName;
	private String lastName;
	private String login;
	private String password;
	private String confirmPassword;

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors actionErrors = new ActionErrors();

		if (firstName == null || lastName == null || firstName.trim() == null
				|| lastName.trim() == null) {
			actionErrors.add("username", new ActionMessage("error.username"));
		}

		if (login == null) {
			actionErrors.add("login", new ActionMessage("error.login.empty"));
		}

		if (password == null || !password.equals(confirmPassword)) {
			actionErrors.add("password", new ActionMessage(
					"error.repeatpassword"));
		}

		return actionErrors;
	}

	@Override
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		this.login = null;
		this.password = null;
		this.confirmPassword = null;
		this.firstName = null;
		this.lastName = null;

		super.reset(mapping, request);
	}

}
